package demo1.java;



import demo1.java.Init.Test1;



import demo1.java.baseDemo.StudentService;
import demo1.java.baseDemo.Teacher;
import demo1.java.controller.TeacherController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-04-16
 * Time: 20:56
 */

// 五大类注解中的四大类都是 @Component 的"子类", 都是针对于@Component的扩展, 都包含了@component注解
public class RunWay {
    public static void main(String[] args) {
//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("spring-config.xml");
//        StudentController studentController =
//                // 1. 使用类注解时, 第一个字符大写且第二个字符小写, 使用小驼峰
//                context.getBean("studentController", StudentController.class);
//        studentController.say();
//
//        // 2. 特殊情况下, 前两个字符都是大写, 则采用原类名
//        STudent sTudent = context.getBean("STudent", STudent.class);
//        sTudent.say();
//
//        StudentService studentService = context.getBean("studentService", StudentService.class);
//        studentService.say();
//
//        Teacher teacher = context.getBean("Teacher", Teacher.class);
//        teacher.say();

        // 1. 属性注入
//        TeacherController controller = context.getBean("teacherController", TeacherController.class);
//        controller.say();



        // 演示XML初始化 bean的生命周期
        // 因为实现了
//        ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-config.xml");
//        XmlInit testInit1 =  context1.getBean("xmlInit", XmlInit.class);
//        testInit1.say();
//        testInit1.doPreDestroy();
//        System.out.println("-----------");
//
//        //演示前置初始化 bean的生命周期
//        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//        TestInit testInit = context.getBean("testInit", TestInit.class);
//        testInit.say();
//        testInit.doPreDestroy();

        System.out.println("----------");

        // 因为 ClassPathXmlApplicationContext 为一次加载
        // 在初始化容器的时候执行了一次
        ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("spring-config.xml");
        // getBean的时候也执行了一次
        Test1 test1 = context2.getBean("test1", Test1.class);
        test1.say();
        test1.doPreDestroy();
    }
}
